package com.example.onlinemarket.graphics;

import android.content.Context;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;

import com.example.onlinemarket.R;
import com.example.onlinemarket.fragment.UserFragment;
import com.example.onlinemarket.util.HttpUtil;

import java.util.HashMap;
import java.util.Map;

import static com.example.onlinemarket.Application.loginUser;

public class UserInfoDialog extends AlertDialog {

    public TextView titleTv;
    public TextView infoTv;
    public EditText infoEt;
    public String info;
    private Button submitBtn;
    private TextView tipTv;
    private UserFragment.RefreshListener listener;

    protected UserInfoDialog(@NonNull Context context, String info, UserFragment.RefreshListener listener) {
        super(context);
        this.info = info;
        this.listener = listener;
    }

    public static UserInfoDialog newInstance(@NonNull Context context, String title, String infoText, String info, UserFragment.RefreshListener listener) {
        UserInfoDialog dialog = new UserInfoDialog(context, info, listener);
        dialog.create();
        dialog.setCancelable(true);
        dialog.titleTv.setText(title);
        dialog.infoTv.setText(infoText);
        dialog.infoEt.setText(info);
        if(title.equals("修改邮箱地址"))
            dialog.submitBtn.setOnClickListener(v -> {
                dialog.dismiss();
                String emailText = dialog.infoEt.getText().toString().trim();
                loginUser.setEmail(emailText);
                listener.refreshEmail();
                new Thread(() -> {
                    Map<String, String> map = new HashMap<>();
                    map.put("userName", loginUser.getUserName());
                    map.put("email", emailText);
                    HttpUtil.sendHttpRequest("/update/user/email", "POST", map);
                }).start();
            });
        else if(title.equals("修改收货地址"))
            dialog.submitBtn.setOnClickListener(v -> {
                dialog.dismiss();
                String address = dialog.infoEt.getText().toString().trim();
                loginUser.setAddress(address);
                listener.refreshAddress();
                new Thread(() -> {
                    Map<String, String> map = new HashMap<>();
                    map.put("userName", loginUser.getUserName());
                    map.put("email", address);
                    HttpUtil.sendHttpRequest("/update/user/address", "POST", map);
                }).start();
            });
        return dialog;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //设置布局
        setContentView(R.layout.dialog_update_user_info);
        //初始化组件
        initWidgets();
        //为组件添加事件监听器
        setWidgetsListener();
    }

    //为组件添加事件监听器
    private void setWidgetsListener() {
        infoEt.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                String input = infoEt.getText().toString().trim();
                if(input.isEmpty())
                    tipTv.setText("请输入信息");
                else{
                    if(input.equals(info))
                        submitBtn.setEnabled(false);
                    else
                        submitBtn.setEnabled(true);
                }
            }
        });
    }

    //初始化组件
    private void initWidgets() {
        titleTv = findViewById(R.id.dialog_title);
        infoTv = findViewById(R.id.tv_dialog_info);
        infoEt = findViewById(R.id.et_dialog_info);
        submitBtn = findViewById(R.id.btn_dialog_submit);
        tipTv = findViewById(R.id.dialog_tip);
    }
}
